/**
 * 沈阳城乡建设委员(施工工程竣工信息)
 *  
 */
import {Base,Item} from '../base';
import { read } from 'fs';
class sggcjgxx extends Base{
  constructor(){
      super();
      this.name='沈阳城乡建设委员(施工工程竣工信息)';
      this.enabled = true;
      this.encode = "utf-8";
      this.debug = false;
      this.timeout = 1000000;
      this.collection = "lnxm";
      this.collectionShowName="辽宁项目爬虫";
      this.keyField = "title";
  }
  getItems(pageIndex:number) {
    let num = pageIndex + 1;
    let url = `http://www.syjs.gov.cn/xxgk/97/102/${num}.aspx`;
    let content = this.spider.getSync(url);
    let $ = this.cheerio.load(content);
    let result :Item[]= [];
    let that = this;
   
    try { let first=$('ul.news_list li').eq(0);
    let second=$('ul.news_list li').eq(1);
       if(num==1){
        let seven=$('ul.news_list li').eq(7);
        $('ul.news_list li').not(first).not(second).not(seven).each(function (index, ele) {
            let title = $(ele).find('h2').find('a').attr('title').trim();
            let url = 'http://www.syjs.gov.cn' + $(ele).find('h2').find('a').attr('href');
            result = result.concat(that.getExtraItem(url, title));
        }); 
       }else{
        let three=$('ul.news_list li').eq(3);
        let four=$('ul.news_list li').eq(4);
        $('ul.news_list li').not(second).not(three).not(four).each(function (index, ele) {
            let title = $(ele).find('h2').find('a').attr('title').trim();
            let url = 'http://www.syjs.gov.cn' + $(ele).find('h2').find('a').attr('href');
            result = result.concat(that.getExtraItem(url, title));
        });
       }
    }
    catch (error) {
        console.error(error);
    }
    return result;
}

getExtraItem(url:string, title:string) {
    let result :Item[]= [];
    let content = this.spider.getSync(url);
    let $ = this.cheerio.load(content);
    let zero = $('table tr').first();
    let one = $('table tr').eq(1);
    let second = $('table tr').eq(2);
    let three = $('table tr').eq(3);
    let that = this;
    let flag = false;
    try {
        let remark = null;
        let remark1:any = null;
        if ($('table tr').eq(1).text().trim().substring(0,4) == '填报单位') {
            remark = $('table tr').not(zero).not(one).not(second).not(three);
            remark1 = second;
        }
        else {
            remark = $('table tr').not(zero).not(one).not(second);
            remark1 = one;
        }
        remark.each(function (index, ele) {
            if($(ele).find('td').eq(3).text().trim()!='合计'){
             let item = that.buildItem();
            remark1.find('td').each(function (index1:any, ele1:any) {
               
                if ((<any>$(ele1)).attr('colspan') == 4) {
                    let s=$(ele).find('td').eq(4).text().trim() || $(ele).find('td').eq(5).text().trim() || $(ele).find('td').eq(6).text().trim() || $(ele).find('td').eq(7).text().trim();
                    (<any>item)[$(ele1).text().trim()] = s;
                    flag = true;
                }
                else {
                    if (flag == false) {
                        console.log($(ele1).text().trim()+'*******************'+$(ele).find('td').eq(index1).text().trim());
                        (<any>item)[$(ele1).text().trim()] = $(ele).find('td').eq(index1).text().trim();
                    }
                    else {
                        console.log($(ele1).text().trim()+'^^^^^^^^^^^^^^^'+ $(ele).find('td').eq(index1+3).text().trim());
                        (<any>item)[$(ele1).text().trim()] = $(ele).find('td').eq(index1+3).text().trim();
                    }
                }
              
            });
            (<any>item).url = url;
            item.title = title;
            item.source = that.name;
            result.push(item);
            flag=false;
        }
        });
    }
    catch (error) {
    }
    return result;
}
  getPageCount ():number{
      return 2;
  }
}
module.exports=new sggcjgxx();